﻿using System.Data;
using System.Data.SqlClient;
using System.Text;
using WorkOrder.Model;

namespace WorkOrder.Business
{
    public class Business_Table_Notice_type
    {
        private DbHelper dbhelper;
        public Business_Table_Notice_type(){dbhelper=new DbHelper();}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int TypeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Table_Notice_Type");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
			parameters[0].Value = TypeID;

			return dbhelper.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model_Table_Notice_Type model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Table_Notice_Type(");
			strSql.Append("TypeName,Remark)");
			strSql.Append(" values (");
			strSql.Append("@TypeName,@Remark)");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.TypeName;
			parameters[1].Value = model.Remark;

			return dbhelper.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public int Update(Model_Table_Notice_Type model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Table_Notice_Type set ");
			strSql.Append("TypeName=@TypeName,");
			strSql.Append("Remark=@Remark");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4),
					new SqlParameter("@TypeName", SqlDbType.NVarChar,50),
					new SqlParameter("@Remark", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.TypeID;
			parameters[1].Value = model.TypeName;
			parameters[2].Value = model.Remark;

			return dbhelper.ExecuteSql(strSql.ToString(),parameters);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public void Delete(int TypeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Table_Notice_Type ");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
			parameters[0].Value = TypeID;

			dbhelper.ExecuteSql(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model_Table_Notice_Type GetModel(int TypeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 TypeID,TypeName,Remark from Table_Notice_Type ");
			strSql.Append(" where TypeID=@TypeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@TypeID", SqlDbType.Int,4)};
			parameters[0].Value = TypeID;

            Model_Table_Notice_Type model = new Model_Table_Notice_Type();
			DataSet ds=dbhelper.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["TypeID"].ToString()!="")
				{
				    model.TypeID = ds.Tables[0].Rows[0]["TypeID"].ToString();
				}
				model.TypeName=ds.Tables[0].Rows[0]["TypeName"].ToString();
				model.Remark=ds.Tables[0].Rows[0]["Remark"].ToString();
				return model;
			}
		    return null;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select TypeID,TypeName,Remark ");
			strSql.Append(" FROM Table_Notice_Type ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return dbhelper.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top);
			}
			strSql.Append(" TypeID,TypeName,Remark ");
			strSql.Append(" FROM Table_Notice_Type ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return dbhelper.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Table_Notice_Type";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

    }
}